﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelectingStudents
{
    class Test
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>()
            {
                new Student("Panayot1", "Popov" , "0000065","09993333" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 1),
                new Student("Ivan", "Spasov" , "12322222","0999112" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 2),
                new Student("Doncho", "Minkov" , "12322222","0999222" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 1),
                new Student("Panayot", "Popov" , "12322222","0999334" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 1),
                new Student("Panayot", "Popov" , "123222222","0223344" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 2),
                new Student("Panayot", "Popov" , "123222222","0999444" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 2),
                new Student("Panayot", "Popov" , "12322222","0999444" , "p2p@abv.bg" , new List<int>() { 2 , 3 , 4 ,5} , 1),
                new Student("Panayot", "Popov" , "12322222","0999444" , "pp@gmail.com" , new List<int>() { 2 , 3 , 4 ,5} , 1),
                new Student("zzz", "Popov" , "1232222","09993332" , "pp@gmail.com" , new List<int>() { 2 , 6 , 4 ,6} , 1),
                new Student("Doncho", "Minkov" , "1232222","02323333" , "pp@gmail.com" , new List<int>() { 2 , 3 , 6 ,5} , 2),
            };

            List<Group> groups = new List<Group>() { new Group("Mathematics", 2), new Group("Literature", 1) };

            var studentsFromGroup2 =
                from stud in students
                where stud.Group == 2
                orderby stud.FirstName
                select stud;

            foreach (var item in studentsFromGroup2)
            {
                Console.WriteLine(item.FirstName);
            }

            var studentsFromGroup2Ex = students.Where(stud => stud.Group == 2).OrderBy(stud => stud.FirstName);

            foreach (var item in studentsFromGroup2Ex)
            {
                Console.WriteLine(item.FirstName);
            }

            var studentsWithAbv = students.Where(stud=>stud.Mail.Contains("abv.bg"));

            foreach (var item in studentsWithAbv)
            {
                Console.WriteLine(item.FirstName + ' '+item.Mail);
            }

            var studentsWithPhonesInSofia = students.Where(stud => stud.Tel.StartsWith("02"));

            foreach (var item in studentsWithPhonesInSofia)
            {
                Console.WriteLine(item.FirstName + ' '+item.Tel);
            }

            var studentsWithExcellect =
                from stud in students
                from mark in stud.Marks
                where mark == 6
                select new { FirstName = stud.FirstName, Marks = stud.Marks };

            foreach (var studd in studentsWithExcellect)
            {
                Console.WriteLine(studd.FirstName);
                foreach (var mark in studd.Marks)
                {
                    Console.Write(mark +",");
                }
            }

            var studentsWith2Excellents =
                from stud in students
                where stud.Marks.Count(mark=>mark == 6) == 2
                select stud;
            Console.WriteLine();
            foreach (var item in studentsWith2Excellents)
            {
                Console.WriteLine(item.FirstName);
            }

            var studentsThatEnroled2006 =
                from stud in students
                where stud.Fn[4] == '0' && stud.Fn[5] == '6'
                select stud;

            foreach (var item in studentsThatEnroled2006)
            {
                Console.WriteLine(item.FirstName);
            }

            var studentsInMathematics =
                from stud in students
                join grup in groups on stud.Group equals grup.GroupNumber
                where grup.DepartmentName == "Mathematics"
                select stud;
            Console.WriteLine();
            foreach (var item in studentsInMathematics)
            {
                Console.WriteLine(item.FirstName);
            }

            var studentsByGroupName =
                from stud in students
                join gr in groups on stud.Group equals gr.GroupNumber
                group stud by gr.DepartmentName;

            Console.WriteLine();
            foreach (var gr in studentsByGroupName)
            {
                Console.WriteLine(gr.Key);
                foreach (var stud in gr)
                {
                    Console.WriteLine(stud.FirstName+" "+stud.LastName);
                }
                Console.WriteLine();
            }

            var studentsByGroupNameUsingExtensions = students.Join(groups, stud => stud.Group, gr => gr.GroupNumber,
                (studs, grups) => new { StudentName = studs.FirstName + " " + studs.LastName, GroupName = grups.DepartmentName }).GroupBy(gr => gr.GroupName);
            Console.WriteLine();
            foreach (var gr in studentsByGroupNameUsingExtensions)
            {
                Console.WriteLine(gr.Key);
                foreach (var stud in gr)
                {
                    Console.WriteLine(stud.StudentName);
                }
                Console.WriteLine();
            }

          

        }
    }
}
